﻿#include "AbstractButtonPropertyEditEntry.h"
#include "ObjectPropertyEditEntryFactory.h"
#include <QAbstractButton>

#include "IconEditEntry.h"
#include "StringEditEntry.h"
#include "SizeEditEntry.h"
#include "BooleanEditEntry.h"
#include "IntegerEditEntry.h"
#include "ShortcutEditEntry.h"

struct AbstractButtonPropertyEditEntry::AbstractButtonPropertyEditEntryPrivate {
    QPointer<QAbstractButton> button;

    QPointer<IconEditEntry> entryIcon;
    QPointer<SizeEditEntry> entryIconSize;
    QPointer<ShortcutEditEntry> entryShortcut;
    QPointer<BooleanEditEntry> entryCheckable;
    QPointer<BooleanEditEntry> entryChecked;
    QPointer<BooleanEditEntry> entryAutoRepeat;
    QPointer<BooleanEditEntry> entryAutoExclusive;
    QPointer<IntegerEditEntry> entryAutoRepeatDelay;
    QPointer<IntegerEditEntry> entryAutoRepeatInterval;
};

REGISTER_PROPERTY_EDIT_ENTRY(AbstractButton)

AbstractButtonPropertyEditEntry::~AbstractButtonPropertyEditEntry() { }

AbstractButtonPropertyEditEntry::AbstractButtonPropertyEditEntry(QWidget *parent) : PropertyEditEntry{parent} {
    p.reset(new AbstractButtonPropertyEditEntryPrivate);
    initUi();
    retranslateUi();
}

AbstractButtonPropertyEditEntry::AbstractButtonPropertyEditEntry(void *object, QWidget *parent) : PropertyEditEntry{parent}  {
    p.reset(new AbstractButtonPropertyEditEntryPrivate);
    initUi();
    retranslateUi();
    setObject(object);
}

void AbstractButtonPropertyEditEntry::setObject(void *object) {
    p->button = static_cast<QAbstractButton*>(object);
    Q_ASSERT(Q_LIKELY(p->button));
}

void AbstractButtonPropertyEditEntry::onTick() {
    const auto &button = p->button;
    if(!button || !isVisible()) {
        return ;
    }

    p->entryIcon->setValue(button->icon());
    p->entryIconSize->setValue(button->iconSize());
    p->entryShortcut->setValue(button->shortcut());
    p->entryCheckable->setValue(button->isCheckable());
    p->entryChecked->setValue(button->isChecked());
    p->entryAutoRepeat->setValue(button->autoRepeat());
    p->entryAutoExclusive->setValue(button->autoExclusive());
    p->entryAutoRepeatDelay->setValue(button->autoRepeatDelay());
    p->entryAutoRepeatInterval->setValue(button->autoRepeatInterval());

    p->entryChecked->setEnabled(button->isCheckable());
}

void AbstractButtonPropertyEditEntry::initUi() {
    setTitle("QAbstractButton");
    // icon
    p->entryIcon = new IconEditEntry;
    appendChildEntry(p->entryIcon);
    // iconSize
    p->entryIconSize = new SizeEditEntry;
    appendChildEntry(p->entryIconSize);
    // shortcut
    p->entryShortcut = new ShortcutEditEntry;
    appendChildEntry(p->entryShortcut);
    //checkable
    p->entryCheckable = new BooleanEditEntry;
    appendChildEntry(p->entryCheckable);
    //checked
    p->entryChecked = new BooleanEditEntry;
    appendChildEntry(p->entryChecked);
    //autoRepeat
    p->entryAutoRepeat = new BooleanEditEntry;
    appendChildEntry(p->entryAutoRepeat);
    //autoExclusive
    p->entryAutoExclusive = new BooleanEditEntry;
    appendChildEntry(p->entryAutoExclusive);
    //autoRepeatDelay
    p->entryAutoRepeatDelay = new IntegerEditEntry;
    appendChildEntry(p->entryAutoRepeatDelay);
    //autoRepeatInterval
    p->entryAutoRepeatInterval = new IntegerEditEntry;
    appendChildEntry(p->entryAutoRepeatInterval);
}

void AbstractButtonPropertyEditEntry::retranslateUi() {
    p->entryIcon->setTitle(tr("icon"));
    p->entryIconSize->setTitle(tr("iconSize"));
    p->entryShortcut->setTitle(tr("shortcut"));
    p->entryCheckable->setTitle(tr("checkable"));
    p->entryChecked->setTitle(tr("checked"));
    p->entryAutoRepeat->setTitle(tr("autoRepeat"));
    p->entryAutoExclusive->setTitle(tr("autoExclusive"));
    p->entryAutoRepeatDelay->setTitle(tr("autoRepeatDelay"));
    p->entryAutoRepeatInterval->setTitle(tr("autoRepeatInterval"));
}
